home *** CD-ROM | disk | FTP | other *** search
/ PC Media 7 / PC MEDIA CD07.iso / share / prog / cm / tstrin.cpp < prev   
Encoding:
C/C++ Source or Header  |  1994-08-31  |  1.4 KB  |  35 lines

  1. // tstrin.cpp
  2. //
  3. // Example program uses the string class and the binary tree template.
  4. // Strings are read one at a time and inserted into the tree template
  5. // until an 'x' (exit) is typed.  An iterator template is then created
  6. // and used to cycle through the tree printing each item.
  7.  
  8. #include <cm/include/cmtbintr.h>
  9. #include <cm/include/cmstring.h>
  10.  
  11. int main()                                    // Start main.
  12. {
  13.   CmTBinaryTree<CmString> tree;               // Binary tree template.
  14.   CmString                input;              // Input string.
  15.   Bool                    finished = FALSE;   // Boolean data type.
  16.  
  17.   while (!finished)                           // While boolean is true,
  18.   {
  19.     cout << "Enter string (\"x\" when done): " << flush;
  20.     cin  >> input;                            // Read string input.
  21.  
  22.     int lnth = input.length();                // Get string length.
  23.     if (lnth == 1 && input[0] == 'x')         // If "x" then finished.
  24.       finished = TRUE;
  25.  
  26.     else if (lnth > 0)                        // Add string to tree.
  27.       tree.add(input);                        // String is copied.
  28.   }
  29.  
  30.   CmTBinaryTreeIterator<CmString> iterator(tree); // Iterator template.
  31.   while (iterator)                            // While iterator is ok,
  32.     cout << iterator++ << endl;               // print each item.
  33.   return 0;
  34. }
  35.